package com.hedl.practice.diyJava.gather.study_properties.test;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * Java集合库提供的Properties用于读写配置文件.properties。.properties文件可以使用UTF-8编码。
 * 可以从文件系统、classpath或其他任何地方读取.properties文件。
 * 读写Properties时，注意仅使用getProperty()和setProperty()方法，不要调用继承而来的get()和put()等方法
 */
public class Main {
    public static void main(String[] args) throws IOException {
        String f = "/Users/a101/Java-Project/study/Daily-Learning/src/main/resources/setting.Properties";
        Properties properties = new Properties();
        properties.load(new FileInputStream(f));

        /**
         * 用Properties读取配置文件，一共有三步:
         *      创建Properties实例；
         *      调用load()读取文件；
         *      调用getProperty()获取配置;
         */
        String filepath = properties.getProperty("last_open_file");
        String interval = properties.getProperty("auto_save_interval","120");

        System.out.println(filepath);
        System.out.println(interval);

        System.out.println("======================================");
        //从内存读取一个字节流：
        String settings = "# test"+"\n"+"course=Java"+"\n"+"last_open_date=2023-02-22T11:00:00";
        ByteArrayInputStream input = new ByteArrayInputStream(settings.getBytes("UTF-8"));
        Properties props = new Properties();
        props.load(input);

        System.out.println("course:"+props.getProperty("course"));
        System.out.println("last_open_date:"+props.getProperty("last_open_date"));
        System.out.println("last_open_file:"+props.getProperty("last_open_file"));
        System.out.println("auto_save:"+props.getProperty("auto_save","60"));

    }
}
